//
//  CityInfoSelectedBtnsView.m
//  CityInfoChooseView
//
//  Created by 正合适 on 2017/10/12.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "CityInfoSelectedBtnsView.h"

#define BASE_TAG 100
#define MARGIN_R 40
#define MAX_PAGES 3

#define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]
#define kSelectedColor RGBAColor(73,144,226,1)
#define kUnselectedColor RGBAColor(51,51,51,1)

@interface CityInfoSelectedBtnsView ()
/**<#desc#>*/
@property (nonatomic, strong) UIScrollView *mainScrollView;
/**数据源*/
@property (nonatomic, strong) NSMutableArray *datas;
/**m默认请选择按钮*/
@property (nonatomic, strong) UIButton *normalBtn;
/**下标view*/
@property (nonatomic, strong) UIView *indexView;
@end

@implementation CityInfoSelectedBtnsView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultData];
        [self createUI];
    }
    return self;
}

#pragma mark - 初始化数据
- (void)defaultData {
    _datas = [NSMutableArray array];
}

#pragma mark - 初始化界面
- (void)createUI {
    _mainScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.frame.size.width, _mainScrollView.frame.size.height);
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_mainScrollView];
    self.normalBtn.tag = MAX_PAGES+BASE_TAG;
    
    UIColor *lineColor = RGBAColor(200, 200, 200, 1);
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
    topLine.backgroundColor = lineColor;
    [self addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, self.frame.size.width, 0.5)];
    bottomLine.backgroundColor = lineColor;
    [self addSubview:bottomLine];
    
    _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, _mainScrollView.frame.size.height-3, 1, 3)];
    _indexView.backgroundColor = kSelectedColor;
    [_mainScrollView addSubview:_indexView];
}

#pragma mark - 初始化按钮
- (void)createBtnsWith:(NSArray *)titles index:(NSInteger)index {
    for (UIButton *btn in _mainScrollView.subviews) {
        if ([btn isKindOfClass:[UIButton class]] && btn.tag != MAX_PAGES+BASE_TAG) {
            [btn removeFromSuperview];
        }
    }
    float orionalX = 28;
    for (NSInteger i = 0; i < titles.count; i ++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(orionalX, 0, 10, self.frame.size.height)];
        btn.tag = BASE_TAG+i;
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        [btn setTitle:_datas[i] forState:UIControlStateNormal];
        [btn setTitleColor:index == i ? kSelectedColor :kUnselectedColor forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_mainScrollView addSubview:btn];
        [btn sizeToFit];
        btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y, btn.frame.size.width, self.frame.size.height);
        orionalX = btn.frame.size.width+btn.frame.origin.x+MARGIN_R;
        if (i == titles.count-1) {
            self.normalBtn.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+MARGIN_R, self.normalBtn.frame.origin.y, self.normalBtn.frame.size.width, self.normalBtn.frame.size.height);
            _mainScrollView.contentSize = CGSizeMake(btn.frame.size.width+btn.frame.origin.x+28, _mainScrollView.frame.size.height);
        }
    }
    
    // 处理默认请选择按钮
    self.normalBtn.hidden = titles.count == MAX_PAGES?YES:NO;
    if (!self.normalBtn.hidden && index != titles.count) {
        [self.normalBtn setTitleColor:kUnselectedColor forState:UIControlStateNormal];
    } else {
        [self.normalBtn setTitleColor:kSelectedColor forState:UIControlStateNormal];
    }
    // indexview的动画
    if (self.normalBtn.hidden) {
        [self indexViewAnimationWith:index];
    } else {
        if (index == titles.count) {
            [self indexViewAnimationWith:self.normalBtn.tag-BASE_TAG];
        } else {
            [self indexViewAnimationWith:index];
        }
    }
}

#pragma mark - 按钮Action
- (void)btnAction:(UIButton *)sender {
    NSInteger index = sender.tag != MAX_PAGES+BASE_TAG ? sender.tag-BASE_TAG : _datas.count;
    if (self.cityInfoSelectedBtnClickBlock) {
        self.cityInfoSelectedBtnClickBlock(index);
    }
}

- (void)configTitles:(NSArray *)titles index:(NSInteger)index {
    _datas = titles.mutableCopy;
    [self createBtnsWith:titles index:index];
//    [self indexViewAnimationWith:index];
    
}

- (UIButton *)normalBtn {
    if (!_normalBtn) {
        _normalBtn = [[UIButton alloc] initWithFrame:CGRectMake(28, 0, 10, self.frame.size.height)];
        _normalBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_normalBtn setTitle:@"请选择" forState:UIControlStateNormal];
        [_normalBtn setTitleColor:kSelectedColor forState:UIControlStateNormal];
        [_normalBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_mainScrollView addSubview:_normalBtn];
        [_normalBtn sizeToFit];
        _normalBtn.frame = CGRectMake(_normalBtn.frame.origin.x, _normalBtn.frame.origin.y, _normalBtn.frame.size.width, self.frame.size.height);
    }
    return _normalBtn;
}

- (void)indexViewAnimationWith:(NSInteger)currentIndex {
    [_mainScrollView bringSubviewToFront:_indexView];
    UIButton *btn = [_mainScrollView viewWithTag:currentIndex+BASE_TAG];
    [UIView animateWithDuration:0.3 animations:^{
        _indexView.frame = CGRectMake(btn.frame.origin.x, _indexView.frame.origin.y, btn.frame.size.width, _indexView.frame.size.height);
    }];
    if (btn.frame.size.width+btn.frame.origin.x > _mainScrollView.frame.size.width) {
        // 自动滚动到index的位置
        [_mainScrollView setContentOffset:CGPointMake(btn.frame.size.width+btn.frame.origin.x-_mainScrollView.frame.size.width, _mainScrollView.contentOffset.y) animated:YES];
    }
}

@end
